# delimit ;

clear;
*version 13;
set matsize 400;
set more off;


log using "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexEmpirical/Conditional/Year/PredictedValues.log", replace;
cd "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexEmpirical/Conditional/Year";

**************************************************************;
*Author: Jeff Carter                                         *;
*Date: Friday, May 15, 2015                            *;
*Purpose: Simulating Predictions                *;
**************************************************************;




use "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexEmpirical/Conditional/Year/SimData.dta";

postutil clear;

postfile mypost prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 diff_hat0 diff_lo0 diff_hi0
                prob_hat01 lo01 hi01 prob_hat11 lo11 hi11 diff_hat01 diff_lo01 diff_hi01
                delta_hat delta_lo delta_hi using PredictedValues, replace;

noisily display "start";


local a=-1 ;
while `a' < 4 { ;

{;


*termlimited milservice termlimited_mil  parliament civwar  rgdp96pc_gle  growth_gle;
*majpow numbord mm time_mid time2_mid time_3mid;
 
scalar h_Accountable = 0    ;
scalar h_LameDuck=   1 ;
scalar h_Civilian = 0 ;
scalar h_Military =  1;
scalar h_Constant = 1;
scalar h_Parliament = 0;
scalar h_CivilWar = 0;
scalar h_GDPpc = 9445.593;
scalar h_Growth = .0201577;
scalar h_MajorPower = 0;
scalar h_Borders = 2;
scalar h_Mobilization = -.0089829 ;
scalar h_Time = 5; 
scalar h_Female =0 ; 
scalar h_Rivalry = 0 ;



generate x_betahat0 = MG_b1*h_Accountable
    + MG_b2*`a'
    + MG_b3*(h_Accountable*`a')
    + MG_b4*h_Rivalry
    + MG_b5*h_Borders
    + MG_b6*h_Parliament
    + MG_b7*h_Female 
    + MG_b8*h_Time 
    + MG_b9*(h_Time^2)
    + MG_b10*(h_Time^3);

generate x_betahat1 = MG_b1* h_LameDuck
    + MG_b2*`a'
    + MG_b3*(h_LameDuck*`a')
    + MG_b4*h_Rivalry
    + MG_b5*h_Borders
    + MG_b6*h_Parliament
    + MG_b7*h_Female 
    + MG_b8*h_Time 
    + MG_b9*(h_Time^2)
    + MG_b10*(h_Time^3);


gen prob0=1/(1+exp(-x_betahat0));
gen prob1=1/(1+exp(-x_betahat1));

 
gen diff0=prob1-prob0 ;

egen probhat0=mean(prob0) ;
egen probhat1=mean(prob1) ;
egen diffhat0=mean(diff0) ;



generate x_betahat01 =   MG_b1*h_Accountable
    + MG_b2*`a'
    + MG_b3*(h_Accountable*`a')
    + MG_b4*h_Rivalry
    + MG_b5*h_Borders
    + MG_b6*h_Parliament
    + MG_b7*h_Female 
    + MG_b8*h_Time 
    + MG_b9*(h_Time^2)
    + MG_b10*(h_Time^3);
 
generate x_betahat11 =   MG_b1* h_LameDuck
    + MG_b2*`a'
    + MG_b3*(h_LameDuck*`a')
    + MG_b4*h_Rivalry
    + MG_b5*h_Borders
    + MG_b6*h_Parliament
    + MG_b7*h_Female 
    + MG_b8*h_Time 
    + MG_b9*(h_Time^2)
    + MG_b10*(h_Time^3);



gen prob01=1/(1+exp(-x_betahat01));
gen prob11=1/(1+exp(-x_betahat11));

gen diff01=prob11-prob01 ;

egen probhat01=mean(prob01) ;
egen probhat11=mean(prob11) ;
egen diffhat01=mean(diff01) ;



gen delta=diff01-diff0;

egen deltahat=mean(delta);




tempname prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 diff_hat0 diff_lo0 diff_hi0
         prob_hat01 lo01 hi01 prob_hat11 lo11 hi11 diff_hat01 diff_lo01 diff_hi01
         delta_hat delta_lo delta_hi;

    _pctile prob0, p(2.5,97.5) ;
    scalar `lo0'=r(r1) ;
    scalar `hi0'=r(r2) ;

    _pctile prob1, p(2.5,97.5) ;
    scalar `lo1'=r(r1) ;
    scalar `hi1'=r(r2) ;

    _pctile diff0, p(2.5,97.5) ;
    scalar `diff_lo0'= r(r1) ;
    scalar `diff_hi0'=r(r2) ;


    scalar `prob_hat0'=probhat0 ;
    scalar `prob_hat1'=probhat1 ;
    scalar `diff_hat0'=diffhat0 ;


       _pctile prob01, p(2.5,97.5) ;
    scalar `lo01'=r(r1) ;
    scalar `hi01'=r(r2) ;

    _pctile prob11, p(2.5,97.5) ;
    scalar `lo11'=r(r1) ;
    scalar `hi11'=r(r2) ;

    _pctile diff01, p(2.5,97.5) ;
    scalar `diff_lo01'= r(r1) ;
    scalar `diff_hi01'=r(r2) ;


    scalar `prob_hat01'=probhat01 ;
    scalar `prob_hat11'=probhat11 ;
    scalar `diff_hat01'=diffhat01 ;


    _pctile delta, p(2.5,97.5) ;
    scalar `delta_lo'= r(r1) ;
    scalar `delta_hi'=r(r2) ;

    scalar `delta_hat'=deltahat;


    post mypost (`prob_hat0') (`lo0') (`hi0') (`prob_hat1') (`lo1') (`hi1') (`diff_hat0') (`diff_lo0') (`diff_hi0')
                (`prob_hat01') (`lo01') (`hi01') (`prob_hat11') (`lo11') (`hi11') (`diff_hat01') (`diff_lo01') (`diff_hi01')
                (`delta_hat') (`delta_lo') (`delta_hi')

    ;

    } ;

    drop    x_betahat0 x_betahat1 prob0 prob1 diff0 probhat0 probhat1 diffhat0
            x_betahat01 x_betahat11 prob01 prob11 diff01 probhat01 probhat11 diffhat01
            delta deltahat;

    local a=`a'+ 1 ;

    display "." _c ;
    } ;
display "" ; postclose mypost ;



use PredictedValues, clear;
gen ruler = _n;
sum;

saveold PredictedValues, replace;


log close;


twoway (rline diff_lo0 diff_hi0 ruler);
